package scenario;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.imageio.ImageIO;

import terrain.HighWallRect;
import terrain.LowWallRect;
import unit.*;
import equipment.*;
import main.*;

public class SmallWar extends Game {
	public SmallWar() {
		Board b = new Board(36, 36);
		if (!UI.buildingForOnline) {
			File imFile = new File("scenario/Plains_1080_1080.png");
			try {
				b.background = ImageIO.read(imFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// For loading from text
		/*else {
			b.background = Utils.decodeToImage(ImageTexts.backStrs1080x1080);
		}*/
		// Make the castle
		double castleSize = 2;
		LowWallRect l1 = new LowWallRect((int)(castleSize*(2+2*Math.sqrt(3) + 2)*Utils.pixelsPerInch), 
											(int)(castleSize*(0 + 2)*Utils.pixelsPerInch),
											0.3*Utils.pixelsPerInch, 
											2*castleSize*Utils.pixelsPerInch, 
											1*Math.PI/6);
		LowWallRect l2 = new LowWallRect((int)(castleSize*(1+2*Math.sqrt(3) + 2)*Utils.pixelsPerInch), 
											(int)(castleSize*(Math.sqrt(3) + 2)*Utils.pixelsPerInch),
											0.3*Utils.pixelsPerInch, 
											2*castleSize*Utils.pixelsPerInch, 
											2*Math.PI/6);
		LowWallRect l3 = new LowWallRect((int)(castleSize*(1+Math.sqrt(3) + 2)*Utils.pixelsPerInch), 
											(int)(castleSize*(1+Math.sqrt(3) + 2)*Utils.pixelsPerInch),
											0.3*Utils.pixelsPerInch, 
											2*castleSize*Utils.pixelsPerInch, 
											1*Math.PI/6);
		HighWallRect h2 = new HighWallRect((int)(castleSize*(Math.sqrt(3) + 2)*Utils.pixelsPerInch), 
											(int)(castleSize*(1+2*Math.sqrt(3) + 2)*Utils.pixelsPerInch),
											0.3*Utils.pixelsPerInch, 
											2*castleSize*Utils.pixelsPerInch, 
											2*Math.PI/6);
		HighWallRect h1 = new HighWallRect((int)(castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch), 
											(int)(castleSize*(0)*Utils.pixelsPerInch),
											0.3*Utils.pixelsPerInch, 
											castleSize*(2/Math.cos(Math.PI/12))*Utils.pixelsPerInch, 
											Math.PI/12);	
		
		b.addTerrain(l1);
		b.addTerrain(l2);
		b.addTerrain(l3);
		b.addTerrain(h2);
		b.addTerrain(h1);
		
		// Make some random trees
		Random rng = new Random();
		ArrayList<Point2D> treeLocs = new ArrayList<Point2D>();
		int numTrees = rng.nextInt(30)+30;
		for (int i = 0; i < numTrees; i++) {
			double x = rng.nextInt(36) * Utils.pixelsPerInch;
			double y = rng.nextInt(36) * Utils.pixelsPerInch;
			Point2D p = new Point2D.Double(x,y);
			if (Math.sqrt(x*x + y*y) > castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch
					&& !treeLocs.contains(p)) {
				treeLocs.add(p);
				double treeSize = Math.max(rng.nextDouble()*Utils.pixelsPerInch/2, Utils.pixelsPerInch/4);
				HighWallRect ht = new HighWallRect(x, y, treeSize, treeSize, rng.nextDouble()*Math.PI);
				b.addTerrain(ht);
			}
			else {
				i--;
			}
		}
		
		
		board = b;
		b.game = this;
		
		// Good units - 80 pts (+heroes)
		ArrayList<Unit> good = new ArrayList<Unit>();
		int numElfArchers = 4;
		int numElfSwordsmen = 4;
		int goodID = 0;
		for (int i = 0; i < numElfArchers; i++) {
			Unit u = new HighElf(goodID++,0,0,0);
			u.equip(new ElfBow());
			good.add(u);
		}
		for (int i = 0; i < numElfSwordsmen; i++) {
			Unit u = new HighElf(goodID++,0,0,0);
			u.equip(new ElvenBlade());
			good.add(u);
		}
		HighElfCaptain capt1 = new HighElfCaptain(goodID++, 0,0,0);
		capt1.equip(new HeavyArmor());
		capt1.equip(new ElvenBlade());
		good.add(capt1);
		HighElfCaptain capt2 = new HighElfCaptain(goodID++, 0,0,0);
		capt2.equip(new HeavyArmor());
		capt2.equip(new ElfBow());
		good.add(capt2);
		
		
		// Bad units - 90 pts (+heroes)
		ArrayList<Unit> bad = new ArrayList<Unit>();
		int numGoblinArchers = 6;
		int numGoblinShield = 6;
		int numGoblinSpear = 6;
		int badID = goodID + 10;
		for (int i = 0; i < numGoblinArchers; i++) {
			Unit u = new Goblin(badID++,1,0,0);
			u.equip(new OrcBow());
			bad.add(u);
		}
		for (int i = 0; i < numGoblinShield; i++) {
			Unit u = new Goblin(badID++,1,0,0);
			u.equip(new Shield());
			bad.add(u);
		}
		for (int i = 0; i < numGoblinSpear; i++) {
			Unit u = new Goblin(badID++,1,0,0);
			u.equip(new Spear());
			bad.add(u);
		}
		GoblinCaptain gc1 = new GoblinCaptain(badID++, 1,0,0);
		gc1.equip(new Shield());
		bad.add(gc1);
		GoblinCaptain gc2 = new GoblinCaptain(badID++, 1,0,0);
		gc2.equip(new Shield());
		bad.add(gc2);
		GoblinCaptain gc3 = new GoblinCaptain(badID++, 1,0,0);
		gc3.equip(new OrcBow());
		bad.add(gc3);
		
		
		Army goodArmy = new Army(good, this);
		Army badArmy = new Army(bad, this);
		goodArmy.addToGame(this);
		badArmy.addToGame(this);
		armies[0] = goodArmy;
		armies[1] = badArmy;
		armiesPlaced = false;
		battles = new ArrayList<Battle>();
		priority = 0;
		currArmy = 0;
		currPhase = "Move";
		
		Shape goodShape = new Ellipse2D.Double(-1*castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch-100,
										-1*castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch-100,
										2*castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch+50,
										2*castleSize*(2+2*Math.sqrt(3) + 2 + 2*Math.tan(Math.PI/12))*Utils.pixelsPerInch+50);
		goodStart = new Area(goodShape);
		
		Shape badShape = new Rectangle2D.Double(30*Utils.pixelsPerInch,0,6*Utils.pixelsPerInch, 36*Utils.pixelsPerInch);
		badStart = new Area(badShape);
		badStart.add(new Area(new Rectangle2D.Double(0,30*Utils.pixelsPerInch,30*Utils.pixelsPerInch,6*Utils.pixelsPerInch)));
	}
}
